﻿using XYAC.Framework.MonitorTireMaterialOnline.DataExporter;

namespace XYAC.Framework.MonitorTireMaterialOnline.Entities
{
    public class SpecValLocal : SpecValTable
    {
        /// <summary>
        /// 解码状态
        /// </summary>
        public bool DecoupState { get; set; } = false;
        /// <summary>
        /// 最后同步时间
        /// </summary>
        public DateTime? LastSyncDate { get; internal set; } = DateTime.Now;
        public override bool? IsDeleted { get; set; }
        /// <summary>
        /// 关联的胎侧实体
        /// </summary>
        public SemiSteelSideInspectionReport SteelSideInspection { get; set; }
        /// <summary>
        /// 关联的报表信息
        /// </summary>
        public SemiSteelTreadScanReport SteelTreadScan { get; internal set; }

        public SpecValLocal() { }
        // 通过 SpecValTable 初始化的构造函数
        public SpecValLocal(SpecValTable table)
        {
            if (table == null)
                throw new ArgumentNullException(nameof(table));

            this.ID = table.ID;
            this.BZ = table.BZ;
            this.StartTime = table.StartTime;
            this.ProfileName = table.ProfileName;
            this.ProfileType = table.ProfileType;
            this.RimHeight = table.RimHeight;
            this.RimTol = table.RimTol;
            this.TotalWidth = table.TotalWidth;
            this.WidthTol = table.WidthTol;
            this.AreaDiff = table.AreaDiff;
            this.TextNo = table.TextNo;
            this.ShoulderWidth = table.ShoulderWidth;
            this.ShoulderWidthTol = table.ShoulderWidthTol;
            this.ShoulderHeight = table.ShoulderHeight;
            this.ShoulderHeightTol = table.ShoulderHeightTol;
            this.jiao = table.jiao;
            this.DateTimePicker = table.DateTimePicker;
            this.GJD = table.GJD;
            this.ResultPercent = table.ResultPercent;
            this.IntervalWidth = table.IntervalWidth;
            this.IXReference = table.IXReference;
            this.SpecVal = table.SpecVal;
            this.BottomHeightVals = table.BottomHeightVals;
            this.KeyPoints = table.KeyPoints;
            this.TestResult = table.TestResult;
            this.AlarmPoints = table.AlarmPoints;
            this.BanZu = table.BanZu;

        }

        public T To<T>() where T : ReportEntityBase
        {
            if (typeof(T) == typeof(SemiSteelTreadScanReport))
            {
                var DataFile = DataExporter.DataFile.FromSpecVal(this);
                //计算出最接近中心线的点
                var IxReferenceStablePoint = DataFile.SpecStable.KeyPoints.OrderBy(T => Math.Abs(T.Point.X - DataFile.XReference)).FirstOrDefault();
                var IxReferencePointIndex = DataFile.SpecStable.KeyPoints.IndexOf(IxReferenceStablePoint);
                var IxReferenceMeasurePoint = DataFile.MeasureKeys[IxReferencePointIndex];

                return new SemiSteelTreadScanReport()
                {
                    //BatchNo = UploadViewModel.CurrentUserChoicePlan.InspectModel.SERIAL_NUMBER
                    //valid
                    SpecName = this.ProfileName,
                    TotalArea = DataFile.Area,//总面积
                    TotalLeftArea = DataFile.LeftArea,
                    TotalRightArea = DataFile.RightArea,
                    //valid
                    MeasureArea = DataFile.MeasureArea,
                    MeasureLeftArea = DataFile.MeasureLeftArea,
                    MeasureRightArea = DataFile.MeasureRightArea,
                    AreaPencent = Math.Abs(Math.Round(((DataFile.MeasureArea - DataFile.Area) / DataFile.Area) * 100F, 2)),

                    //valid
                    TotalWidth_Stable = DataFile.StableKeys.LastOrDefault().Coordinate,
                    TotalWidth_Scan = DataFile.MeasureKeys.LastOrDefault().Coordinate,
                    TotalWidth_Symmetry = Math.Abs(DataFile.XReference - Math.Abs(DataFile.MeasureKeys.LastOrDefault().Coordinate - DataFile.XReference)),
                    //valid
                    CrownWidth_Stable = Math.Abs(DataFile.StableKeys[DataFile.StableKeys.Count - 2].Coordinate - DataFile.StableKeys[1].Coordinate),
                    CrownWidth_Scan = Math.Abs(DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Coordinate - DataFile.MeasureKeys[1].Coordinate),
                    CrownWidth_Symmetry = Math.Abs(Math.Abs(DataFile.XReference - DataFile.MeasureKeys[1].Coordinate) - (DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Coordinate - DataFile.XReference)),
                    //valid
                    ShoulderThickness_Stable = DataFile.StableKeys[1].Value,
                    ShoulderThickness_ScanLeft = DataFile.MeasureKeys[1].Value,
                    ShoulderThickness_ScanRight = DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Value,
                    ShoulderThickness_Symmetry = Math.Abs(DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Value - DataFile.MeasureKeys[1].Value),
                    //valid
                    CrownThickness_Stable = IxReferenceStablePoint.Point.Y,
                    CrownThickness_Scan = IxReferenceMeasurePoint.Value,
                    //valid
                    BeadWingTipThickness_ScanLeft = DataFile.MeasureKeys.FirstOrDefault().Value,
                    BeadWingTipThickness_ScanRight = DataFile.MeasureKeys.LastOrDefault().Value,
                    //valid
                    BeadWingWidth_Stable = Math.Abs(DataFile.StableKeys[1].Coordinate - DataFile.StableKeys[0].Coordinate),
                    BeadWingWidth_ScanLeft = Math.Abs(DataFile.MeasureKeys[1].Coordinate - DataFile.MeasureKeys[0].Coordinate),
                    BeadWingWidth_ScanRight = Math.Abs(DataFile.MeasureKeys.LastOrDefault().Coordinate - DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Coordinate),
                    BeadWingWidth_Symmetry = Math.Abs(Math.Abs(DataFile.MeasureKeys[1].Coordinate - DataFile.MeasureKeys[0].Coordinate) - Math.Abs(DataFile.MeasureKeys.LastOrDefault().Coordinate - DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Coordinate)),

                } as T;
            }
            else if (typeof(T) == typeof(SemiSteelSideInspectionReport))
            {

                var DataFile = XYAC.Framework.MonitorTireMaterialOnline.DataExporter.DataFile.FromSpecVal(this);
                return new SemiSteelSideInspectionReport()
                {
                    //BatchNo = UploadViewModel.CurrentUserChoicePlan.InspectModel.SERIAL_NUMBER
                    SpecName = this.ProfileName,
                    //valid
                    TotalArea = DataFile.Area,//总面积
                    TotalLeftArea = DataFile.LeftArea,
                    TotalRightArea = DataFile.RightArea,
                    //valid
                    MeasureArea = DataFile.MeasureArea,
                    MeasureLeftArea = DataFile.MeasureLeftArea,
                    MeasureRightArea = DataFile.MeasureRightArea,
                    AreaPencent = Math.Abs(Math.Round(((DataFile.MeasureArea - DataFile.Area) / DataFile.Area) * 100F, 2)),

                    //valid
                    TotalWidth_Stable = DataFile.StableKeys.LastOrDefault().Coordinate,
                    TotalWidth_Scan = DataFile.MeasureKeys.LastOrDefault().Coordinate,
                    TotalWidth_Symmetry = Math.Abs(DataFile.XReference - (DataFile.StableKeys.LastOrDefault().Coordinate - DataFile.XReference)),
                    ////胎侧厚度扫描
                    //SidewallThickness_Stable = Math.Abs(DataFile.StableKeys[DataFile.StableKeys.Count - 2].Coordinate - DataFile.StableKeys[1].Coordinate),
                    //SidewallThickness_ScanLeft = Math.Abs(DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Coordinate - DataFile.MeasureKeys[1].Coordinate),
                    //SidewallThickness_ScanRight = Math.Abs(Math.Abs(DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Coordinate - DataFile.XReference) - Math.Abs(DataFile.MeasureKeys[1].Coordinate - DataFile.XReference)),
                    //SidewallThickness_Symmetry = DataFile.StableKeys[1].Value,
                    ////RC胶厚度标准
                    //RCRubberThickness_Stable = DataFile.MeasureKeys[1].Value,
                    //RCRubberThickness_ScanLeft = DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Value,
                    //RCRubberThickness_ScanRight = Math.Abs(DataFile.MeasureKeys[DataFile.MeasureKeys.Count - 2].Value - DataFile.MeasureKeys[1].Value),
                    //RCRubberThickness_Symmetry = DataFile.StableKeys[(DataFile.StableKeys.Count - 1) / 2].Value,


                } as T;
            }
            return null;
        }
    }
}
